<?php
class description extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('mdescriptioncontent');
		$this->load->model('msetsubmenu');
	}
	
	function index()
	{
		$data['active'] = "description";
		$config['base_url'] = base_url().'description/index/';
		$config['total_rows'] = $this->db->count_all('description_content');
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;
		
		$config['full_tag_open'] = '<div><ul class="pagination pagination-sm">';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->mdescriptioncontent->getList($config['per_page'],$this->uri->segment(3));
			$this->load->view('description/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add(){
		$data['active'] = "description";
		$data['mode'] = "Tambah";
		$data['action'] = "save";
		$data['submenu'] = $this->msetsubmenu->getComboList(1);
		$this->load->view('description/form',$data);
	}
	
	function edit($id_description){
		$data['active'] = "description";
		$data['mode'] = "Ubah";
		$data['action'] = "update";
		$data['submenu'] = $this->msetsubmenu->getComboList(1);
		$data['detail'] = $this->mdescriptioncontent->detail($id_description);
		$this->load->view('description/form',$data);
	}
	
	function save()
	{
		$id_description = $this->input->post('id_description');
		$id_submenu = $this->input->post('id_submenu');
		$title_description = $this->input->post('title_description');
		$date_description = date('Y-m-d');
		$text_description = $this->input->post('text_description');
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$config['upload_path'] = './upload/description';
			$config['allowed_types'] = 'jpg|png|gif';
			$this->load->library('upload', $config);
			$this->upload->do_upload('file_description');
			$getFile = $this->upload->data();
			$file_description = $getFile['file_name'];
		
			$this->mdescriptioncontent->setData($id_description,$id_submenu,$title_description,$date_description,$text_description,$file_description);
			$this->mdescriptioncontent->create();
			$this->session->set_flashdata('success', true);
			redirect('description');
		}
		$this->session->set_flashdata('error', true);
		redirect('description');
	}
	
	function update()
	{
		$id_description = $this->input->post('id_description');
		$id_submenu = $this->input->post('id_submenu');
		$title_description = $this->input->post('title_description');
		$date_description = $this->input->post('date_description');
		$text_description = $this->input->post('text_description');
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			if(!$this->input->post('file_description')){
				$config['upload_path'] = './upload/description';
				$config['allowed_types'] = 'jpg|png|gif';
				$this->load->library('upload', $config);
				$this->upload->do_upload('file_description');
				$getFile = $this->upload->data();
				$file_description = $getFile['file_name'];
			}else{
				$file_description = $this->input->post('file_description');
			}
		
			$this->mdescriptioncontent->setData($id_description,$id_submenu,$title_description,$date_description,$text_description,$file_description);
			$this->mdescriptioncontent->update($id_description);
			$this->session->set_flashdata('success', true);
			redirect('description');
		}
		$this->session->set_flashdata('error', true);
		redirect('description');
	}
	
	function delete($id_description)
	{
		$getHapus = $this->mdescriptioncontent->detail($id_description);
		
		if(@unlink("upload/description/".$getHapus[0]['file_description']) || $getHapus[0]['file_description'] == "0" || $getHapus[0]['file_description'] == ""){
			if ($this->mdescriptioncontent->remove($id_description)){
				$this->session->set_flashdata('delete', true);
				redirect('description','refresh');
			}
		}
		$this->session->set_flashdata('error', true);
		redirect('description');
	}	
	
	function searchData(){
		$data['active'] = "description";
		$name = $this->input->post('name');
		$data['results'] = $this->mdescriptioncontent->getListSearch($name);
		$this->load->view('description/list',$data);
	}
}
?>